﻿
namespace Patterns4Net.PatternEnforcer.MSBuildTask
{
    using System.IO;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;

    /// <summary>
    /// Implements the Pattern Enforcer task for MSBuild.
    /// </summary>
    public class PatternEnforcerTask : ToolTask
    {
        [Required]
        public ITaskItem[] InputAssemblies { get; set; }

        public string ConfigFile { get; set; }

        public string OutputFile { get; set; }

        public bool FailOnError { get; set; }
		
		public bool ShowErrorsAsWarnings { get; set; }

        protected override string GenerateFullPathToTool()
        {
            return Path.Combine(this.ToolPath, this.ToolName);
        }

        public override bool Execute()
        {
            var result = base.Execute();
            return this.FailOnError || result;
        }

        protected override string ToolName
        {
            get { return "pattern-enforcer.exe"; }
        }

        protected override string GenerateCommandLineCommands()
        {
            var builder = new CommandLineBuilder();

            if (string.IsNullOrWhiteSpace(this.ConfigFile) == false)
            {
                builder.AppendSwitch("-config-file ");
                builder.AppendTextUnquoted(this.ConfigFile + " ");
            }

            builder.AppendSwitch("-output-format ");
			if (this.ShowErrorsAsWarnings)
			{
				builder.AppendTextUnquoted("vs-warnings ");
			}
			else
			{
				builder.AppendTextUnquoted("vs ");
			}

            builder.AppendFileNamesIfNotNull(this.InputAssemblies, " ");

            return builder.ToString();
        }
    }
}
